package com.jt.assistant.repository

import com.jt.assistant.base.BaseResult
import com.jt.assistant.http.Authorization
import com.jt.assistant.http.RetrofitClient
import com.jt.assistant.api.ApiService
import com.jt.assistant.bean.*
import com.tencent.mmkv.MMKV
import com.yzp.mvvmlibrary.base.BaseModel

class LoginRepository private constructor(
) : BaseModel() {

    private val mService by lazy { RetrofitClient.getInstance().create(ApiService::class.java) }

    suspend fun login(param: Param): BaseResult<LoginDataBean> {
        return mService.login(
            Authorization.getInstance().authorization,
            param
        )
    }

    suspend fun getUpgradeUlr(): BaseResult<UpdateBean> {
        return mService.getUpgradeUlr(
            Authorization.getInstance().authorization,
            "http://40.73.59.226:9001/app/getUpgradeUlr"
        )
    }

    suspend fun getUpgrade(url: String,appInfo: AppInfo): BaseResult<UpdateAppBean> {
        return mService.getUpgrade(
            Authorization.getInstance().authorization,
            getToken(),
            url,
            appInfo
        )
    }



    companion object {
        @Volatile
        private var INSTANCE: LoginRepository? = null
        fun getInstance() =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: LoginRepository().also { INSTANCE = it }
            }
    }


    private fun getToken(): String? {
        return if (MMKV.defaultMMKV().getString("TOKEN", "").isNullOrEmpty()) {
            "bearer"
        } else {
            "bearer " + (MMKV.defaultMMKV().getString("TOKEN", ""))
        }
    }

}